package cn.sangedon.review.analysis.strategy.specific;

import cn.sangedon.review.analysis.constant.StrategyEnum;
import cn.sangedon.review.analysis.service.StockStasticsService;
import cn.sangedon.review.common.domain.analysis.StockStastics;
import cn.sangedon.review.common.domain.analysis.StockStrategy;
import cn.sangedon.review.common.domain.analysis.StockStrategyResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

/**
 * 今日之前连续缩量下跌三天以上且今日继续缩量，但是上涨的股票
 */
@Slf4j
@Component
public class ContinuousShrinkRiseStrategy extends AbstractScheduleTradeStrategy {
    @Autowired
    private StockStasticsService stockStasticsService;

    @Override
    public void runJob(List<StockStrategy> strategyList) {
        strategyList.stream().filter(e -> e.getStrategyName().equals(StrategyEnum.CONTINUOUS_SHRINK_RISE.getName())).findFirst()
                    .ifPresent(e -> {
                        addSchedule(e);
                    });
    }

    @Override
    public void execute() {
        log.info("开始执行连续缩量下跌涨策略");
        List<StockStastics> stockStastics = stockStasticsService.continuousShrinkRise();
        if (CollectionUtils.isEmpty(stockStastics)) {
            log.info("无符合连续缩量下跌涨策略股票");
            return;
        }
        List<StockStastics> preStockStastics = stockStasticsService.list(
            new LambdaQueryWrapper<StockStastics>().eq(StockStastics::getTradeDate, stockStastics.get(0).getTradeDate()));

        Map<String, StockStastics> preStasticsMap = preStockStastics.stream().collect(
            Collectors.toMap(StockStastics::getStockCode, Function.identity()));

        List<StockStrategyResult> list = new ArrayList<>();
        for (StockStastics stockStastic : stockStastics) {
            if (CollectionUtils.isEmpty(preStasticsMap)) {
                break;
            }
            StockStastics stastics = preStasticsMap.get(stockStastic.getStockCode());
            if (stastics == null || stastics.getFallDay() < 3 || stastics.getReduceVolumeDay() < 3) {
                continue;
            }
            StockStrategyResult strategyResult = new StockStrategyResult();
            strategyResult.setStrategyId(stockStrategy.getId());
            strategyResult.setStrategyName(stockStrategy.getStrategyName());
            strategyResult.setStockName(stockStastic.getStockName());
            strategyResult.setStockCode(stockStastic.getStockCode());
            strategyResult.setTradingDate(stockStastic.getTradeDate());
            list.add(strategyResult);
        }
        stockStrategyResultService.saveBatch(list);
        log.info("符合连续缩量下跌涨的股票数量:{}", list.size());
    }
}
